package com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetCategoryManagement;
import com.xyht.sca_s.student_manage_system.modules.asset.entity.SmsAssetFixedStorage;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetCategoryManagementMapper;
import com.xyht.sca_s.student_manage_system.modules.asset.mapper.SmsAssetFixedStorageMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.SmsAssetInventory;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.SmsAssetInventoryDetail;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.SmsAssetInventoryPlan;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.req.SmsAssetInventoryDetailReq;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.req.SmsAssetInventoryPlanFinishReq;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.resp.SmsAssetInventoryDetailListResp;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.entity.resp.SmsAssetInventoryPlanResp;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.mapper.SmsAssetInventoryDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.mapper.SmsAssetInventoryMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.mapper.SmsAssetInventoryPlanMapper;
import com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.service.SmsAssetInventoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.assetFixedSelfInspection.entity.resp.SmsAssetFixedStorageInfoResp;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.entity.resp.SmsSearchUserResp;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import com.xyht.sca_s.student_manage_system.modules.websocket.entity.WebsocketResult;
import com.xyht.sca_s.student_manage_system.modules.websocket.service.WebSocketService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedInventory.constant.MyAssetInventoryTypeConstant.*;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedSelfInspection.constant.IsCompletedConstant.COMPLETED_TYPE_NO;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedSelfInspection.constant.IsCompletedConstant.COMPLETED_TYPE_YES;
import static com.xyht.sca_s.student_manage_system.modules.assetFixedSelfInspection.constant.IsUseConstant.*;

import static com.xyht.sca_s.student_manage_system.modules.websocket.myEnum.WebsocketResultEnum.ASSET_INVENTORY_STATUS_UPDATE;

/**
 * <p>
 * 我的资产 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-09-29
 */
@Service
public class SmsAssetInventoryServiceImpl extends ServiceImpl<SmsAssetInventoryMapper, SmsAssetInventory> implements SmsAssetInventoryService {

    @Resource
    private SmsAssetInventoryMapper smsAssetInventoryMapper;
    @Resource
    private SmsAssetInventoryPlanMapper smsAssetInventoryPlanMapper;
    @Resource
    private SmsAssetInventoryDetailMapper smsAssetInventoryDetailMapper;
    @Resource
    private SmsAssetFixedStorageMapper smsAssetFixedStorageMapper;
    @Resource
    private SmsUserMapper smsUserMapper;
    @Resource
    private SmsAssetCategoryManagementMapper smsAssetCategoryManagementMapper;
    @Resource
    private WebSocketService webSocketService;

    @Override
    public ResponseResult getMyPlanList(String userId, Integer pagNum, Integer pageSize) {
        List<SmsAssetInventoryPlanResp> list = new ArrayList<>();
        Page<SmsAssetInventoryPlan> page = new Page<>(pagNum, pageSize);
        //先查是否有自查计划表开启并在时间范围内
        Date nowDate = new Date();
        List<SmsAssetInventoryPlan> planList = smsAssetInventoryPlanMapper.selectList(new LambdaQueryWrapper<SmsAssetInventoryPlan>()
                .eq(SmsAssetInventoryPlan::getIsUse, IS_USE_YES)//启用
                .lt(SmsAssetInventoryPlan::getBgTime, nowDate) //开始时间小于当前时间
                .gt(SmsAssetInventoryPlan::getEndTime, nowDate)); //结束时间大于当前时间
        if (planList == null || planList.size() < 1) {
            return CommonResult.success(list);
        }
        //获取计划id
        List<String> planIdList = planList.stream().map(SmsAssetInventoryPlan::getId).collect(Collectors.toList());
        //查询计划下该用户的资产自查计划id集合
        List<String> filterPlanIdList = smsAssetInventoryMapper.selectList(new LambdaQueryWrapper<SmsAssetInventory>()
                        .eq(SmsAssetInventory::getUserId, userId)
                        .in(SmsAssetInventory::getPlanId, planIdList))
                .stream()
                .map(SmsAssetInventory::getPlanId)
                .collect(Collectors.toList());
        if (filterPlanIdList.isEmpty()) {
            return CommonResult.success(list);
        }
        smsAssetInventoryPlanMapper.selectPage(page, new LambdaQueryWrapper<SmsAssetInventoryPlan>()
                .in(SmsAssetInventoryPlan::getId, filterPlanIdList)
                .orderByAsc(SmsAssetInventoryPlan::getIsComplete)
                .orderByDesc(SmsAssetInventoryPlan::getCreateTime));
        list = page.getRecords().stream()
                .map(smsAssetInventoryPlan -> {
                    SmsAssetInventoryPlanResp resp = new SmsAssetInventoryPlanResp();
                    BeanUtils.copyProperties(smsAssetInventoryPlan, resp);
                    resp.setPlanId(smsAssetInventoryPlan.getId());
//                    //是否完成
//                    List<SmsAssetInventory> assetInventoryList = smsAssetInventoryMapper.selectList(new LambdaQueryWrapper<SmsAssetInventory>()
//                            .eq(SmsAssetInventory::getPlanId, smsAssetInventoryPlan.getId())
//                            .eq(SmsAssetInventory::getUserId, userId));
//                    if (assetInventoryList != null && assetInventoryList.size() > 0) {
//                        resp.setIsComplete(assetInventoryList.get(0).getIsComplete());
//                        resp.setCompleteTime(assetInventoryList.get(0).getCompleteTime());
//                    }else {
//                        resp.setIsComplete(COMPLETED_TYPE_NO);
//                    }
                    //完成数量/未完成数量
                    Integer sunCount = smsAssetInventoryDetailMapper.selectCount(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                            .eq(SmsAssetInventoryDetail::getPlanId, smsAssetInventoryPlan.getId()));//总数
                    Integer completedCount = smsAssetInventoryDetailMapper.selectCount(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                            .eq(SmsAssetInventoryDetail::getPlanId, smsAssetInventoryPlan.getId())
                            .eq(SmsAssetInventoryDetail::getIsComplete, COMPLETED_TYPE_YES));//已完成数
                    resp.setCompleteCount(completedCount);
                    resp.setNotCompleteCount(sunCount - completedCount);
                    return resp;
                })
                .collect(Collectors.toList());

        return CommonResult.success(list, (int) page.getTotal());
    }

    @Override
    public ResponseResult getInventoryAssetList(Integer pagNum, Integer pageSize, String userId, String planId, String searchStr, Integer assetStatus, Integer isComplete, String assetId, Integer type, String inventoryDetailId) {
        if (isNullOrEmpty(planId,type)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        List<SmsAssetInventoryDetailListResp> inventoryDetailList = new ArrayList<>();

        //必要的校验
        SmsAssetInventoryPlan smsAssetInventoryPlan = smsAssetInventoryPlanMapper.selectById(planId);
        if (isNullOrEmpty(smsAssetInventoryPlan)) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INSPECTION_PLAN_NOT_EXIST);
        }
        Integer isUse = smsAssetInventoryPlan.getIsUse();
        Date bgTime = smsAssetInventoryPlan.getBgTime();
        Date endTime = smsAssetInventoryPlan.getEndTime();
        if (isUse == IS_USE_NO || bgTime.compareTo(new Date()) > 0 || endTime.compareTo(new Date()) < 0) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INSPECTION_PLAN_NOT_START);
        }
        //校验该用户是否有自查计划
        List<SmsAssetInventory> inspectionList = smsAssetInventoryMapper.selectList(new LambdaQueryWrapper<SmsAssetInventory>()
                .eq(SmsAssetInventory::getPlanId, planId)
                .eq(SmsAssetInventory::getUserId, userId));
        if (inspectionList == null || inspectionList.isEmpty()) {
            return CommonResult.success(inventoryDetailList);
        }

        LambdaQueryWrapper<SmsAssetInventoryDetail> selfInspectionDetailLambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                .eq(SmsAssetInventoryDetail::getPlanId, planId)
                .orderByAsc(SmsAssetInventoryDetail::getIsComplete)
                .orderByDesc(SmsAssetInventoryDetail::getCreateTime);

        if (!isNullOrEmpty(searchStr)) {
            List<String> assetIdList = smsAssetFixedStorageMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedStorage>()
                    .and(i -> {
                        i.like(SmsAssetFixedStorage::getAssetName, searchStr)
                                .or()
                                .like(SmsAssetFixedStorage::getAssetModel, searchStr)
                                .or()
                                .like(SmsAssetFixedStorage::getAssetBrand, searchStr);
                    })).stream().map(SmsAssetFixedStorage::getId).collect(Collectors.toList());
            if (!assetIdList.isEmpty()) {
                selfInspectionDetailLambdaQueryWrapper.in(SmsAssetInventoryDetail::getAssetId, assetIdList);
            }else {
                selfInspectionDetailLambdaQueryWrapper.eq(SmsAssetInventoryDetail::getAssetId, "");
            }
        }

        if (!isNullOrEmpty(assetStatus)) {
            selfInspectionDetailLambdaQueryWrapper.eq(SmsAssetInventoryDetail::getAssetStatus, assetStatus);
        }

        if (!isNullOrEmpty(assetId)) {
            selfInspectionDetailLambdaQueryWrapper.eq(SmsAssetInventoryDetail::getAssetId, assetId);
        }
        if (!isNullOrEmpty(inventoryDetailId)) {
            selfInspectionDetailLambdaQueryWrapper.eq(SmsAssetInventoryDetail::getId, inventoryDetailId);
        }
        if (!isNullOrEmpty(isComplete)) {
            selfInspectionDetailLambdaQueryWrapper.eq(SmsAssetInventoryDetail::getIsComplete, isComplete);
        }
        int total;
        Page<SmsAssetInventoryDetail> page = new Page<>(pagNum, pageSize);
        if (type == MY_ASSET_INVENTORY_TYPE_INVENTORIED) {
            smsAssetInventoryDetailMapper.selectPage(page,selfInspectionDetailLambdaQueryWrapper
                    .eq(SmsAssetInventoryDetail::getUserId, userId)
                    .eq(SmsAssetInventoryDetail::getIsComplete,COMPLETED_TYPE_YES));
            inventoryDetailList = page.getRecords()
                    .stream()
                    .map(smsAssetInventoryDetail -> {
                        SmsAssetInventoryDetailListResp resp1 = new SmsAssetInventoryDetailListResp();
                        BeanUtils.copyProperties(smsAssetInventoryDetail, resp1);
                        SmsAssetFixedStorageInfoResp assetInfo = new SmsAssetFixedStorageInfoResp();
                        SmsAssetFixedStorage assetFixedStorage = smsAssetFixedStorageMapper.selectById(smsAssetInventoryDetail.getAssetId());
                        if (!isNullOrEmpty(assetFixedStorage)) {
                            BeanUtils.copyProperties(smsAssetInventoryDetail, resp1);
                        }
                        resp1.setAssetInfo(assetInfo);
                        return resp1;
                    })
                    .collect(Collectors.toList());
            total = (int) page.getTotal();

        } else if (type == MY_ASSET_INVENTORY_TYPE_NOT_INVENTORIED) {//我未盘点的列表

            smsAssetInventoryDetailMapper.selectPage(page,selfInspectionDetailLambdaQueryWrapper
                    .eq(SmsAssetInventoryDetail::getIsComplete,COMPLETED_TYPE_NO));
            inventoryDetailList = page.getRecords()
                    .stream()
                    .map(smsAssetInventoryDetail -> {
                        SmsAssetInventoryDetailListResp resp1 = new SmsAssetInventoryDetailListResp();
                        BeanUtils.copyProperties(smsAssetInventoryDetail, resp1);
                        SmsAssetFixedStorageInfoResp assetInfo = new SmsAssetFixedStorageInfoResp();
                        SmsAssetFixedStorage assetFixedStorage = smsAssetFixedStorageMapper.selectById(smsAssetInventoryDetail.getAssetId());
                        if (!isNullOrEmpty(assetFixedStorage)) {
                            BeanUtils.copyProperties(assetFixedStorage, assetInfo);
                        }
                        resp1.setAssetInfo(assetInfo);
                        return resp1;
                    })
                    .collect(Collectors.toList());
            total = (int) page.getTotal();
        }else {
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }


        return CommonResult.success(inventoryDetailList, total);
    }

    @Override
    @Transactional
    public ResponseResult finishOneAsset(String userId, SmsAssetInventoryDetailReq req) {
        if (isNullOrEmpty(req.getPlanId(), req.getAssetId(), req.getInventoryPicture(),
                req.getAssetStatus())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        //校验计划
        SmsAssetInventoryPlan assetInventoryPlan = smsAssetInventoryPlanMapper.selectById(req.getPlanId());
        if (isNullOrEmpty(assetInventoryPlan)) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INSPECTION_PLAN_NOT_EXIST);
        }
        //校验是否有资格
        List<SmsAssetInventory> assetInventoryList = smsAssetInventoryMapper.selectList(new LambdaQueryWrapper<SmsAssetInventory>()
                .eq(SmsAssetInventory::getPlanId, req.getPlanId())
                .eq(SmsAssetInventory::getUserId, userId));
        if (assetInventoryList == null || assetInventoryList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INVENTORY_NOT_EXIST);
        }
        List<SmsAssetInventoryDetail> assetInventoryDetailList = smsAssetInventoryDetailMapper.selectList(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                .eq(SmsAssetInventoryDetail::getAssetId, req.getAssetId())
                .eq(SmsAssetInventoryDetail::getPlanId, req.getPlanId()));
        if (assetInventoryDetailList == null || assetInventoryDetailList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INSPECTION_DETAIL_NOT_EXIST);
        }
        //先校验是否有人已经盘点
        Integer isComplete = assetInventoryDetailList.get(0).getIsComplete();
        if (isComplete == COMPLETED_TYPE_YES) {
            return CommonResult.failed(CommonCodeEnum.ASSET_INVENTORY_ALREADY_COMPLETE);
        }


        Integer isOpenScan = assetInventoryPlan.getIsOpenScan();

        SmsAssetInventoryDetail inventoryDetail = assetInventoryDetailList.get(0);
        inventoryDetail.setCompleteTime(new Date());
        inventoryDetail.setInventoryId(assetInventoryList.get(0).getId());
        inventoryDetail.setIsComplete(COMPLETED_TYPE_YES);
        inventoryDetail.setInventoryPicture(req.getInventoryPicture());
        inventoryDetail.setAssetStatus(req.getAssetStatus());
        inventoryDetail.setRemark(req.getRemark());
        inventoryDetail.setUserId(userId);
        if (isOpenScan != null && isOpenScan == IS_SCAN_YES) {
            if (req.getIsScanCode() == IS_SCAN_YES) {//有扫描码
                if (isNullOrEmpty(req.getAssetScanCode())) {
                    return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
                }
                if (req.getAssetId().equals(req.getAssetScanCode())) {
                    inventoryDetail.setIsScanCode(IS_SCAN_YES);
                    inventoryDetail.setInventoryIsScan(COMPLETED_TYPE_YES);
                }else {
                    return CommonResult.failed(CommonCodeEnum.ASSET_SCAN_CODE_ERROR);
                }
            }else {
                inventoryDetail.setIsScanCode(IS_SCAN_NO);
            }
        }

        smsAssetInventoryDetailMapper.updateById(inventoryDetail);
        //检验是否全部盘点完,修改状态
        //先查是否有人盘点完
        Integer sunCount = smsAssetInventoryDetailMapper.selectCount(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                .eq(SmsAssetInventoryDetail::getPlanId, req.getPlanId()));//总的盘点数量
        Integer completeCount = smsAssetInventoryDetailMapper.selectCount(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                .eq(SmsAssetInventoryDetail::getPlanId, req.getPlanId())
                .eq(SmsAssetInventoryDetail::getIsComplete, COMPLETED_TYPE_YES));//已经盘点的数量
        if (sunCount.equals(completeCount)) {
            //修改每个人的盘点状态
            SmsAssetInventory smsAssetInventory = new SmsAssetInventory();
            smsAssetInventory.setCompleteTime(new Date());
            smsAssetInventory.setIsComplete(COMPLETED_TYPE_YES);
            smsAssetInventoryMapper.update(smsAssetInventory, new LambdaQueryWrapper<SmsAssetInventory>()
                    .eq(SmsAssetInventory::getPlanId, req.getPlanId()));
            //修改盘点计划状态
            assetInventoryPlan.setIsComplete(COMPLETED_TYPE_YES);
            assetInventoryPlan.setCompleteTime(new Date());
            smsAssetInventoryPlanMapper.updateById(assetInventoryPlan);
        }
        //webSocket通知
        List<String> userIdList = smsAssetInventoryMapper.selectList(new LambdaQueryWrapper<SmsAssetInventory>()
                .eq(SmsAssetInventory::getPlanId, req.getPlanId()))
                .stream()
                .map(SmsAssetInventory::getUserId)
                .collect(Collectors.toList());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId",userId);
        jsonObject.put("assetId",inventoryDetail.getId());
        webSocketService.batchSend(userIdList, null, WebsocketResult.success(ASSET_INVENTORY_STATUS_UPDATE,jsonObject.toString()));
        return CommonResult.success();
    }


    @Override
    public ResponseResult finishAll(String userId, SmsAssetInventoryPlanFinishReq finishReq) {
        if (isNullOrEmpty(finishReq.getPlanId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        SmsAssetInventoryPlan assetInventoryPlan = smsAssetInventoryPlanMapper.selectById(finishReq.getPlanId());
        if (isNullOrEmpty(assetInventoryPlan)) {
            return CommonResult.failed(CommonCodeEnum.ASSET_SELF_INSPECTION_PLAN_NOT_EXIST);
        }
        String planId = assetInventoryPlan.getId();
        //先查是否有人盘点完
        List<SmsAssetInventoryDetail> detailFilterList = smsAssetInventoryDetailMapper.selectList(new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                        .eq(SmsAssetInventoryDetail::getPlanId, planId))
                .stream()
                .filter(i -> i.getIsComplete().equals(COMPLETED_TYPE_NO))
                .collect(Collectors.toList());
        if (detailFilterList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.ASSET_INVENTORY_ALREADY_COMPLETE);
        }
        SmsAssetInventory smsAssetInventory = new SmsAssetInventory();
        smsAssetInventory.setCompleteTime(new Date());
        smsAssetInventory.setIsComplete(COMPLETED_TYPE_YES);
        smsAssetInventoryMapper.update(smsAssetInventory, new LambdaQueryWrapper<SmsAssetInventory>()
                .eq(SmsAssetInventory::getPlanId, planId));

        return CommonResult.success();
    }

    @Override
    public ResponseResult getInventoryAssetAllList(Integer pagNum, Integer pageSize, String planId, String searchStr, Integer assetStatus, Integer isComplete, String inventoryUserName) {
        if (isNullOrEmpty(planId)) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        Page<SmsAssetInventoryDetail> page = new Page<>(pagNum, pageSize);
        LambdaQueryWrapper<SmsAssetInventoryDetail> lambdaQueryWrapper = new LambdaQueryWrapper<SmsAssetInventoryDetail>()
                .eq(SmsAssetInventoryDetail::getPlanId, planId)
                .orderByAsc(SmsAssetInventoryDetail::getIsComplete)
                .orderByDesc(SmsAssetInventoryDetail::getCreateTime);
        if (!isNullOrEmpty(searchStr)) {
            List<String> assetIdList = smsAssetFixedStorageMapper.selectList(new LambdaQueryWrapper<SmsAssetFixedStorage>()
                    .and(i -> {
                        i.like(SmsAssetFixedStorage::getAssetName, searchStr)
                                .or()
                                .like(SmsAssetFixedStorage::getAssetModel, searchStr)
                                .or()
                                .like(SmsAssetFixedStorage::getAssetBrand, searchStr);
                    })).stream().map(SmsAssetFixedStorage::getId).collect(Collectors.toList());
            if (!assetIdList.isEmpty()) {
                lambdaQueryWrapper.in(SmsAssetInventoryDetail::getAssetId, assetIdList);
            }else {
                lambdaQueryWrapper.eq(SmsAssetInventoryDetail::getAssetId, "");
            }
        }

        if (!isNullOrEmpty(isComplete)) {
            lambdaQueryWrapper.eq(SmsAssetInventoryDetail::getIsComplete, isComplete);
        }

        if (!isNullOrEmpty(assetStatus)) {
            lambdaQueryWrapper.eq(SmsAssetInventoryDetail::getAssetStatus, assetStatus);
        }
        if (!isNullOrEmpty(inventoryUserName)) {
            List<String> userIdList = smsUserMapper.selectList(new LambdaQueryWrapper<SmsUser>()
                    .like(SmsUser::getRealName, inventoryUserName))
                    .stream()
                    .map(SmsUser::getId)
                    .collect(Collectors.toList());
            if (!userIdList.isEmpty()) {
                lambdaQueryWrapper.in(SmsAssetInventoryDetail::getUserId, userIdList);
            }else {
                lambdaQueryWrapper.in(SmsAssetInventoryDetail::getUserId, "");
            }
        }
        smsAssetInventoryDetailMapper.selectPage(page, lambdaQueryWrapper);
        List<SmsAssetInventoryDetailListResp> assetInventoryDetailRespList = page.getRecords()
                .stream()
                .map(smsAssetInventoryDetail -> {
                    SmsAssetInventoryDetailListResp detailResp = new SmsAssetInventoryDetailListResp();
                    BeanUtils.copyProperties(smsAssetInventoryDetail, detailResp);
                    SmsAssetFixedStorageInfoResp assetInfo = new SmsAssetFixedStorageInfoResp();
                    SmsSearchUserResp userInfo = new SmsSearchUserResp();
                    //资产信息
                    SmsAssetFixedStorage smsAssetFixedStorage = smsAssetFixedStorageMapper.selectById(smsAssetInventoryDetail.getAssetId());
                    if (!isNullOrEmpty(smsAssetFixedStorage)) {
                        BeanUtils.copyProperties(smsAssetFixedStorage, assetInfo);
                        //类别
                        SmsAssetCategoryManagement smsAssetCategoryManagement = smsAssetCategoryManagementMapper.selectById(smsAssetFixedStorage.getAssetCategory());
                        if (!isNullOrEmpty(smsAssetCategoryManagement)) {
                            assetInfo.setAssetCategoryName(smsAssetCategoryManagement.getCategoryName());
                            assetInfo.setAssetCategory(smsAssetCategoryManagement.getId());
                        }
                    }
                    //盘点信息
                    SmsUser smsUser = smsUserMapper.selectById(smsAssetInventoryDetail.getUserId());
                    if (!isNullOrEmpty(smsUser)) {
                        BeanUtils.copyProperties(smsUser, userInfo);
                    }

                    detailResp.setAssetInfo(assetInfo);
                    detailResp.setInventoryUserInfo(userInfo);
                    return detailResp;
                })
                .collect(Collectors.toList());
        return CommonResult.success(assetInventoryDetailRespList, (int)page.getTotal());
    }
}
